﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Frame.Ctrl
{
    public abstract class BaseNode
    {

        public bool IsWorking { get { return m_IsWorking; } }
        private bool m_IsWorking;
        /// <summary>
        /// 是否启用更新
        /// </summary>
        public abstract bool IsUpdate { get; }

        /// <summary>
        /// 节点类型
        /// </summary>
        public abstract string NodeType { get; }

        public abstract List<string> ListExcute { get; }
        public abstract List<string> ListTryGetValue { get; }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="data"></param>
        public void Initialized(object data) { OnInitialized(data); }

        /// <summary>
        /// 进入
        /// </summary>
        public void Enter()
        {
            m_IsWorking = true;
            NodeManager.Instance.RegstorEnter(this);
            NodeManager.Instance.RegstorExcute(this);
            NodeManager.Instance.RegstorTryGetValue(this);
            if (IsUpdate) NodeManager.Instance.RegstorUpdate(this);
            OnEnter();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        public void Update() { OnUpdate(); }

        /// <summary>
        /// 冻结
        /// </summary>
        public void Freeze()
        {
            m_IsWorking = false;
            if (IsUpdate) NodeManager.Instance.UnRegstorUpdate(this);
            OnFreeze();
        }

        /// <summary>
        /// 取消冻结
        /// </summary>
        public void UnFreeze()
        {
            m_IsWorking = true;
            if (IsUpdate) NodeManager.Instance.RegstorUpdate(this);
            OnUnFreeze();
        }

        /// <summary>
        /// 退出
        /// </summary>
        public void End()
        {
            m_IsWorking = false;            
            if (IsUpdate) NodeManager.Instance.UnRegstorUpdate(this);
            NodeManager.Instance.UnRegstorExcute(this);
            NodeManager.Instance.UnRegstorTryGetValue(this);
            NodeManager.Instance.UnRegstorEnter(this);
            OnEnd();
        }

        /// <summary>
        /// 释放
        /// </summary>
        public void Release()
        {
            m_IsWorking = false;
            if (IsUpdate) NodeManager.Instance.UnRegstorUpdate(this);
            NodeManager.Instance.UnRegstorExcute(this);
            NodeManager.Instance.UnRegstorTryGetValue(this);
            NodeManager.Instance.UnRegstorEnter(this);
            OnRelease();
        }


        public void Excute(string msg, object[] body) { OnExcute(msg, body); }
        public bool TryGetValue(string msg, string key, out object body) { return OnTryGetValue(msg, key, out body); }

        protected abstract void OnInitialized(object data);
        protected abstract void OnEnter();
        protected abstract void OnUpdate();
        protected abstract void OnFreeze();
        protected abstract void OnUnFreeze();
        protected abstract void OnEnd();
        protected abstract void OnRelease();
        protected abstract void OnExcute(string msg, object[] body);
        protected abstract bool OnTryGetValue(string msg, string key, out object body);
    }
}